package com.ch.dream.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

import android.os.Environment;

public class FileHelper {
	//存储卡的位置
	private static String SDPATH = Environment.getExternalStorageDirectory()+"/";

	/*
	 *在SD卡上创建文件
	 *
	 */
	public static File createSDFile(String fileName) throws Exception{
		File file =new File(SDPATH+fileName);
		if(isFileExist(fileName)){
			file.delete();
		}
		file.createNewFile();
		return file;

	}
	/*
	 *在SD卡上创建目录
	 *
	 */
	public static File createSDDir(String dirName) throws Exception{
		File dir =new File(SDPATH+dirName);
		dir.mkdir();
		return dir;
	}
	/*
	 *判断文件是否存在
	 *
	 */
	public static boolean isFileExist(String fileName){
		File file =new File(SDPATH+fileName);
		return file.exists();
	}

	/*
	 * 通过http读取文本信息
	 *
	 */
	public static String getText(String urlStr){
		StringBuffer result =new StringBuffer();
		String temp =null ;
		BufferedReader reader = null;
		URL url;
		try{
			url = new URL(urlStr);
			HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
			reader = new BufferedReader(new InputStreamReader(httpUrl.getInputStream()));
			while((temp=reader.readLine())!=null){
				result.append(temp);
			}

		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				if(reader!=null){
					reader.close();
				}

			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result.toString();
	}
	/*
	 * 将数据流存储到SD卡上
	 *
	 */
	public static File writeToSD(String path,String fileName,InputStream input){
		File file =null;
		OutputStream out =null;
		try{
			createSDDir(path);
			file = createSDFile(path+"/"+fileName);
			out = new FileOutputStream(file);
			byte buffer[] = new byte[4*1024];
			while (input.read(buffer) !=-1) {
				out.write(buffer);
			}
			out.flush();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				out.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return file;
	}
	
	/*
	 * 将字符串存储到SD卡上
	 *
	 */
	public static File writeToSD(String path,String fileName,String str){
		File file =null;
		OutputStream out =null;
		try{
			createSDDir(path);
			file = createSDFile(path+"/"+fileName);
			out = new FileOutputStream(file);
			out.write(str.getBytes());
			out.flush();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				out.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return file;
	}
	/*
	 *获取流
	 *
	 */
	public static InputStream getInputStream(String urlStr){
		URL url;
		try{
			url = new URL(urlStr);
			HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
			return httpUrl.getInputStream();

		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/*
	 * 下载文件到SD卡中，返回值：成功0，失败-1，已存在1
	 */
	public static int download(String urlStr,String path,String fileName,boolean isOver){
		InputStream input =null;
		try{
			if(isFileExist(fileName) && !isOver){
				return 1;
			}else{
				input = getInputStream(urlStr);
				if(input!=null){
					File file =writeToSD(path,fileName,input);
					if(file!=null){
						return 0;
					}else{
						System.out.println("保存到SD卡时出错.");
						return -1;
					}
				}else{
					System.out.println("读取流时出错.");
					return -1;
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	/*
	 *  删除文件
	 */
	public static boolean delete(String path,String fileName){
		File file =new File(SDPATH+path+"/"+fileName);
		try{
			if(isFileExist(path+"/"+fileName)){
				return file.delete();
			}else{
				return true;
			}
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/*
	 *  删除目录
	 */
	public static boolean delete(String path){
		return delete(SDPATH+path,"");
	}
	/*
	 * 读取字符串文件
	 */
	public static String read(String path,String charset) throws Exception{
		BufferedReader reader =new BufferedReader(new InputStreamReader(new FileInputStream(new File(SDPATH+path)),Charset.forName(charset)));
		StringBuilder result =new StringBuilder();
		String line;
		while((line = reader.readLine())!=null){
			result.append(line+"\n");
		}
		return result.toString();
	}
	
	/*
	 * 读取字符串文件第一行
	 */
	public static String readFirstLine(String path,String charset) throws Exception{
		BufferedReader reader =new BufferedReader(new InputStreamReader(new FileInputStream(new File(SDPATH+path)),Charset.forName(charset)));
		StringBuilder result =new StringBuilder();
		String line;
		while((line = reader.readLine())!=null){
			result.append(line);
			return result.toString();
		}
		return result.toString();
		
	}
	
}
